package writeor.aidraw.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import writeor.aidraw.model.User;
import writeor.aidraw.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;


/**
 * 用户控制器类
 */
@Controller
public class UserController {
	// 依赖注入
	@Autowired
	private UserService userService;

	@RequestMapping(value = "/manage/user/person_info")//去个人信息页面
	public String person_info( @RequestParam(value = "page", defaultValue = "1") int page,
									  @RequestParam(value = "count", defaultValue = "6") int count,
							   HttpServletRequest request,HttpSession session) {

        User user = (User) session.getAttribute("USER_SESSION");

		request.setAttribute("user",user);
		request.setAttribute("search","flase");//分页的
		return "/manage/person_info";
	}

	@PostMapping("manage/user/updateUser_self")//修改自己的信息
	@ResponseBody
	public Map updateUser_self(User user,HttpSession session,HttpServletRequest request) {


		int rows = userService.updateUser108(user);
		// 将用户对象添加到Session
		session.invalidate();//清除,会重现登录
//		session.setAttribute("USER_SESSION", user);
//		request.setAttribute("user",user);
		System.out.println("成功修改用户:"+user.getUser_name());

		Map map=new HashMap<>();
		if(rows >0){
			map.put("result","success");
			map.put("msg","用户修改成功");
		}else{
			map.put("result","fail");
			map.put("msg","用户修改失败");
		}
		return map;

	}
	/**
	 * 删除商品分类信息
	 */
	@RequestMapping("manage/user/deleteUser_108/{userid}")
	public String deleteUser(@PathVariable("userid") Integer user_id) {
		int rows = userService.deleteUser_108(user_id);
		//int rows = categoryService.deleteCate(cate_id);
		return "redirect:../getUserList";
	}


	/**
	 * 更新商品分类
	 */
	@PostMapping("manage/user/updateUser108")
	@ResponseBody
	public Map updateUser108(User user) {


		int rows = userService.updateUser108(user);

		Map map=new HashMap<>();
		if(rows >0){
			map.put("result","success");
			map.put("msg","用户修改成功");
		}else{
			map.put("result","fail");
			map.put("msg","用户修改失败");
		}
		return map;

	}
	//搜索用户
	@GetMapping("/manage/user/find_users")
	public String find_goods_byName(@RequestParam(value = "page", defaultValue = "1") int page,
									@RequestParam(value = "count", defaultValue = "6") int count,
									HttpServletRequest request,@RequestParam(value = "find_users") String find_users){
		System.out.println("find_users："+find_users);
		if(find_users==""){
			//搜索为空，重定向
			return "redirect:getUserList";
		}
		PageInfo<User> users = userService.FindUsersByName(page,count,find_users);
		request.setAttribute("userlist",users);
		request.setAttribute("search","true");//分页的
		return "/manage/user_list";
	}

	@GetMapping(value = "/toLogin")
	public  String toLogin(Model model){
		model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
		return "/user/login";
	}
	@PostMapping(value = "/login")
	public  String login(Model model,String username,String password, HttpSession session){
		//System.out.println(username+password);
		// 通过账号和密码查询用户
		User user = userService.findUser(username, password);
		if(user != null){
			// 将用户对象添加到Session
			session.setAttribute("USER_SESSION", user);
			System.out.println("成功查到用户:"+user.getUser_name());
			// 跳转到后台管理主页面
			return "redirect:/";
		}
		model.addAttribute("login_fail", "true");
		// 返回到登录页面

		return "/user/login";
	}
	/**
	 * 用户登录
	 */
//	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	@PostMapping("/login.action")
	public String login(String username,String password, Model model, 
                                                          HttpSession session) {
		// 通过账号和密码查询用户
		User user = userService.findUser(username, password);
		if(user != null){		
			// 将用户对象添加到Session
			session.setAttribute("USER_SESSION", user);
			// 跳转到主页面
			return "redirect:category/list.action";
		}
		model.addAttribute("msg", "账号或密码错误，请重新输入！");
         // 返回到登录页面
		return "login";
	}

	/**
	 * 向用户登陆页面跳转
	 */
//	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	@GetMapping("/login.action")
	public String toLogin() {
	    return "login";
	}
	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/logout")
	public String logout(HttpSession session) {
		// 清除Session
		session.invalidate();
		// 重定向到登录页面的跳转方法
		return "redirect:/";
	}

	/**
	 * 注册
	 */
	@GetMapping (value = "/toRegister")
	public String toRegister(HttpSession session) {

		return "/user/register";
	}
	@PostMapping(value = "/register")
	public  String register(Model model,String username,String password,String email, HttpSession session){
		//System.out.println(username+password);
		// 通过账号和密码查询用户
		User user = new User(null,username,password,email, (byte) 0);


		try {
			int row = userService.createUser(user);
			if(row>0){

				System.out.println("注册成功");
				model.addAttribute("register_suc", "true");
				// 跳转到后台管理主页面
				return "/user/login";
			}
		} catch(Exception e) {
			System.out.println("注册失败");
			model.addAttribute("register_fail", "true");
			return "/user/register";
		}
		// 返回到登录页面

		return "/user/login";
	}


	@RequestMapping(value = "/user/userlist.action", method = RequestMethod.GET)
	public String userlist(Model model) {
		List list=userService.getUserlist();
		model.addAttribute("userlist", list);	
	    return "user_list1";
	}



	@RequestMapping(value = "/user/addUser.action")
	public String addUser(User user) {
		System.out.println(user.toString());
		int i=userService.createUser(user);
		System.out.println("成功创建用户："+i);

	    return "redirect:/user/userlist.action";
	}




	@RequestMapping(value = "manage/user/getUserList")
	public String getUserlistForPage( @RequestParam(value = "page", defaultValue = "1") int page,
									  @RequestParam(value = "count", defaultValue = "6") int count,
									  HttpServletRequest request) {
		PageInfo<User> USerListPage=userService.FindUserListForPage(page,count);
		request.setAttribute("userlist",USerListPage);
		request.setAttribute("search","flase");//分页的
		return "/manage/user_list";
	}

	/**
	 * 新建商品分类
	 */
	@PostMapping("manage/user/addUser108")
	@ResponseBody
	public Map createUser108(User user) {
		System.out.println("create的用户:"+user.toString());
		int rows = userService.createUser108(user);

		Map map=new HashMap<>();
		if(rows >0){
			map.put("result","success");
			map.put("msg","用户添加成功");
		}else{
			map.put("result","fail");
			map.put("msg","用户添加失败");
		}
		return map;
	}

	
}
